package com.wolfalpha.utron.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.adapter.IdeaDirAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义的对话框
 *
 * @author
 */
public class DirsAlertDialog {

    private IdeaDirAdapter adapter;
    private Context context;

    private android.app.AlertDialog ad;

    private TextView titleView;

    private RelativeLayout messageView;

    private TextView sure;

    private TextView cancel;

    private GridView grd;

    private LayoutInflater inflater;

    List<String> strings = new ArrayList<String>();

    private boolean single = false;

    public DirsAlertDialog(Context context, boolean sig) {
        // TODO Auto-generated constructor stub
        this.single = sig;
        this.context = context;
        inflater = LayoutInflater.from(context);
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_dirs);

        grd = (GridView) window.findViewById(R.id.gv_dialog_dir);
        titleView = (TextView) window.findViewById(R.id.tv_dialog_base_title);
        messageView = (RelativeLayout) window.findViewById(R.id.rl_dialog_base_message);
        sure = (TextView) window.findViewById(R.id.tv_dialog_base_sure);
        cancel = (TextView) window.findViewById(R.id.tv_dialog_base_cancel);

        setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 数据
     *
     * @param data
     */
    public void setData(List<String> data) {
        if (data != null) {
            strings = data;
            adapter = new IdeaDirAdapter(context, strings, single);
            grd.setAdapter(adapter);
        }
    }


    /**
     * 获取返回值
     *
     * @return
     */
    public List<Integer> getResult() {
        return adapter.getSelected();
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }


    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text, final View.OnClickListener listener) {
        sure.setText(text);
        sure.setOnClickListener(listener);
    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text, final View.OnClickListener listener) {
        cancel.setText(text);
        cancel.setOnClickListener(listener);
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }

}